package descentFly.app {
	import UIFree.controls.uif_scrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import descentFly.app.scrollBar.df_scrollBar;
	import descentFly.app.scrollBar.df_scrollBarV;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	public class df_list extends uif_ecomponent {
		protected var _scrollArea	: uif_scrollArea;
		protected var _selectIndex	: int;
		protected var _scrollBar	: df_scrollBar;
		
		private	 var _auto_timer: Timer;
		private  var _prevSelectClass: String;
		private	 var _topIndex: int;
		private	 var _itemSize: Number;
		
		public function df_list(parent:uif_component, 
								 ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, 
								 styleClass: Object='list') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_scrollArea 	= createAcrollArea();
			_scrollBar		= createScrollBar();
			_scrollBar.addEvent(Event.CHANGE, doScrollBar);
			_selectIndex 	= -1;
		}
		
		override protected function listenEvents(): void {
			addEvent(MouseEvent.MOUSE_WHEEL, doMWell);
		}
		
		protected function doMWell(e: MouseEvent): void {
			if (!_scrollArea.area.getFX('y'))
				_scrollArea.scrollTo(0, e.delta * _itemSize);
		}
		
		protected function createAcrollArea(): uif_scrollArea {
			var inn: Rectangle = getInner();
			inn.width -= df_component.SCROLLBAR_WIDTH;
			return new uif_scrollArea(this, inn.x, inn.y, inn.width, inn.height, style.area);
		}
		
		protected function createScrollBar(): df_scrollBar {
			var inn: Rectangle = getInner();
			inn.left += inn.width - df_component.SCROLLBAR_WIDTH;
			return new df_scrollBarV(this, inn.x, inn.y, inn.width, inn.height, style.scrollBar);
		}
		
		public function get selectItem(): uif_ecomponent {
			return _scrollArea.area.getChildAt(_selectIndex) as uif_ecomponent;
		}
		
		public function set selectItem(item: uif_ecomponent): void {
			if (item.parent == _scrollArea.area)
				selectIndex = _scrollArea.area.getChildIndex(item);
		}
		
		public function set selectIndex(newIndex: int): void {
			if ((_selectIndex != newIndex) &&
				(_scrollArea.area.numChildren > newIndex)) {
				if (newIndex > -1) {
					var l_selectItem: uif_component = _scrollArea.area.getChildAt(newIndex) as uif_component;
					if (l_selectItem) {
						if (selectIndex > -1) {
							selectItem.fx('style', selectItem.style, app.styleByObj(_prevSelectClass)).start();
//							selectItem.styleClass = _prevSelectClass;
						}
						_selectIndex = newIndex;
						if (style.selectClass) {
							_prevSelectClass = l_selectItem.styleClass;
							l_selectItem.fx('style', l_selectItem.style, app.styleByObj(style.selectClass)).start();
							//l_selectItem.styleClass = style.selectClass;
						}
						
						_scrollArea.scrollToItem(l_selectItem);
					}
				} else {
					if (selectIndex > -1) selectItem.styleClass = _prevSelectClass;
					_selectIndex = newIndex;
				}
				this.dispatchEvent(new Event(Event.CHANGE));
			}
			this.dispatchEvent(new Event(Event.SELECT));
		}
		
		public function get selectIndex(): int {
			return _selectIndex;
		}
		
		public function get list(): Array {
			return _scrollArea.list;
		}
		
		public function clearList(): void {
			if (_scrollArea.list) _scrollArea.list.splice(0, _scrollArea.list.length);
			_scrollArea.area.removeChilds();
			_selectIndex = -1;
			_scrollArea.area.y = 0;
		}
		
		public function removeItem(item: uif_ecomponent): void {
			var sIndex: int = selectIndex; 
			if (item == selectItem) selectIndex = -1;
			_scrollArea.removeItem(item);
			selectIndex = (sIndex < list.length)?sIndex:(list.length - 1);
		}
		
		protected function doScrollBar(e: Event): void {
			_scrollArea.scrollTo(0, -(_scrollBar.value - _topIndex) * _itemSize);
			_topIndex = _scrollBar.value;
		}
		
		public function attachList(list: Array, createFunc:*): df_list {
			function createElem(area: uif_component, item: *, index: int): uif_component {
				var l_item: uif_component = createFunc(area, item, index);
				addEvent(MouseEvent.CLICK, function(e: MouseEvent): void {
					if (!l_item.disable) This.selectItem = l_item;
				}, l_item);
				_itemSize += l_item.height;
				return l_item;
			}
			_scrollArea.attachList(list, createElem);
			_selectIndex 	= -1;
			_itemSize		= _scrollArea.area.getInner().height / list.length;
			var hCount: int = list.length - Math.round(_scrollArea.getInner().height / _itemSize);
			
			_topIndex		= 0;
			if (hCount > 0)  {
				_scrollBar.max 		= hCount;
				_scrollBar.visible 	= true;
			} else _scrollBar.visible = false;
			return this;
		}
	}
}